package com.hjx.windowtool.tablemodel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.swing.table.AbstractTableModel;

public class MapTableModel extends AbstractTableModel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	List<Map<String, String>> list=new ArrayList<>();
	String[] columnNames=new String[] {};

	String[] columnPrepertys=new String[] {};
	public MapTableModel(List<Map<String, String>> list, String[] columnNames, String[] columnPrepertys) {
		super();
		this.list = list;
		this.columnNames = columnNames;
		this.columnPrepertys = columnPrepertys;
	}
	
	@Override
	public int getRowCount() {
		return list.size();
	}
	@Override
	public String getColumnName(int column) {
		if (column==0) {
			return "全选";
		}
		return columnNames[column-1];
	}
	@Override
	public int getColumnCount() {
		return columnNames.length+1;
	}
	@Override
	public Class<?> getColumnClass(int columnIndex) {
		if (columnIndex==0) {
			return Boolean.class;
		}
		return super.getColumnClass(columnIndex);
	}
	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return true;
	}
	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		if (columnIndex==0) {
			return Boolean.valueOf(list.get(rowIndex).getOrDefault("checked", (String) "false").toString());
		}
		return list.get(rowIndex).get(columnPrepertys[columnIndex-1]);
	}
	public List<Map<String, String>> getList() {
		return list;
	}

	public void setList(List<Map<String, String>> list) {
		this.list = list;
		fireTableDataChanged();
	}
}
